/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						      palette_creator.h

	$Header: /resource_editor/palette_creator.h $

	$NoKeywords: $

 ************************************************************************/

#ifndef PALETTE_CREATOR_H_INCLUDED
#define PALETTE_CREATOR_H_INCLUDED

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <vector>
#include "color_count.h"
#include "color_cube.h"

struct t_color_tree
{
	t_axis  axis;
	int     value;
	int     less_than;
	int     greater_or_equal;
	bool    is_leaf;
};

// ---------------------------------------------------------------
// take a 24 bit image and convert it to a palettized version.
// ---------------------------------------------------------------
//------------------------------------------------------------
// palette creator.h
//------------------------------------------------------------
class t_palette_creator
{
public:
	void count( t_pixel_24 const& pixel );
	void create( int max_colors, int reserved_colors, bool force_creation = false );
	int  get_index( t_pixel_24 const& pixel );
	std::vector<t_pixel_24> const& get_palette() const;
protected:
	t_color_count* find( t_pixel_24 const& pixel, bool create_if_missing );

	void set_reserved_colors( int reserved_colors );

	enum { k_hash_size = 7919 };

	std::vector<t_color_count_ptr> m_hash_table[k_hash_size];
	std::vector<t_color_cube>      m_cubes;
	t_color_count_list             m_colors;
	std::vector<t_pixel_24>        m_palette;
	std::vector<t_color_tree>      m_color_tree;
};

inline void t_palette_creator::count( t_pixel_24 const& pixel )
{
	find( pixel, true )->count++;
}

inline std::vector<t_pixel_24> const& t_palette_creator::get_palette() const
{
	return m_palette;
}

#endif // PALETTE_CREATOR_H_INCLUDED